﻿#include<assert.h>
#include<iostream>
using namespace std;
namespace bit
{
    template<class T>
    class vector
    {
    public:
        // Vector的迭代器是一个原生指针
        typedef T* iterator;
        typedef const T* const_iterator;
        iterator begin()
        {
            return _start;
        }

        iterator end()
        {
            return _finish;
        }

        const_iterator cbegin() const
        {
            return _start;
        }

        const_iterator cend() const
        {
            return _finish;
        }

            // construct and destroy
        vector()
        {
            _start = _finish = _endOfStorage = nullptr;
        }

        vector(int n, const T& value = T())
        {
            assert(n);
            iterator temp = new T[n];
            for (int i = 0; i < n; i++)
            {
                temp[i] = value;
            }
            _start = temp;
            _finish = _start + n;
            _endOfStorage = _finish;
        }

        template<class InputIterator>
        vector(InputIterator first, InputIterator last)
        {
            int n = last - first + 1;
            iterator temp = new T[n];
            for (int i = 0; i < n; i++)
            {
                temp[i] =first[i] ;
            }
            _start = temp;
            _finish = _start + n;
            _endOfStorage = _finish;
        }

        vector(const vector<T>& v)
        {
            int n = v.size();
            iterator temp = new T[n];
            for (int i = 0; i < n; i++)
            {
                temp[i] = v[i];
            }
            _start = temp;
            _finish = _start + n;
            _endOfStorage = _finish;
        }
        
        vector<T>& operator= (vector<T> v)
        {
            delete[] _start;
            int n = v.size();
            iterator temp = new T[n];
            for (int i = 0; i < n; i++)
            {
                temp[i] = v[i];
            }
            _start = temp;
            _finish = _start + n;
            _endOfStorage = _finish;
            return *this;
        }

        ~vector()
        {
            delete[] _start;
            _start = _finish = _endOfStorage = nullptr;
        }

            // capacity
        size_t size() const
        {
            return _finish - _start;
        }

        size_t capacity() const
        {
                return _endOfStorage - _start;
        }

        void reserve(size_t n)
        {
            if (n >= size())
            {
                iterator temp = new T[n];
                for (int i = 0; i < size(); i++)
                {
                    temp[i] = _start[i];
                }
                _finish = temp + size();
                _start = temp;
                _endOfStorage = temp + n;
            }
               
        }

        void resize(size_t n, const T& value = T())
        {
            if (n <= size())
            {
                _finish -= size()-n;
            }
            else 
            {
                if (n > capacity())
                {
                    reserve(n);
                }
                for (int i = size(); i < n; i++)
                {
                    _start[i] = value;
                }
                _finish = _start + n;
            }

        }


            ///////////////access///////////////////////////////
        T& operator[](size_t pos)
        {
            return _start[pos] ;
        }

        const T& operator[](size_t pos)const
        {
            return _start[pos];
        }

            ///////////////modify/////////////////////////////
        void push_back(const T& x)
        {
            if (size() == capacity())
            {
                reserve(capacity() == 0 ? 4 : capacity() * 2);
            }
            *(_finish++) = x;
        }

        void pop_back()
        {
            assert(size());
            _finish--;
        }

        void swap(vector<T>& v)
        {
            vector<T> temp = v;
            v = *this;
            *this = temp;
        }

        iterator insert(iterator pos, const T& x)
        {
            int set = pos - _start;
            if (size() == capacity())
            {
                reserve(capacity() == 0 ? 4 : capacity() * 2);
            }
            for (int i = size()-1; i >= set; i--)
            {
                _start[i + 1] = _start[i];
            }
            _start[set] = x;
            _finish++;
            return _start + set;
        }

        iterator erase(iterator pos)
        {
            assert(pos < _finish);
            for (iterator i = pos; i != _finish; i++)
            {
                *i = i[1];
            }
            _finish--;
            return pos;
        }
    private:
        iterator _start; // 指向数据块的开始
        iterator _finish; // 指向有效数据的尾
        iterator _endOfStorage; // 指向存储容量的尾
    };
}